<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/include/taglibs.jsp" %>
<html>
<head>
    <title>新增功能</title>
    <%@ include file="/include/openstyles.jsp" %>
    <script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/jquery.validate.min.js"></script>
    <script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/validate-methods.js"></script>
    <script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/jquery/jquery-form.js"></script>
    <script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/input.js"></script>
    <script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/lui.jiantingdiv.js"></script>
    <script type="text/javascript" src="${ctx}/assets/uc/js/common.js"></script>
    <style>
        .help-block {
            margin: 0px !important;
        }
    </style>
</head>
<body>
<div class="row">
    <div class="col-xs-12">
        <div class="space-12"></div>
        <form class="form-horizontal" role="form" id="function_form">
            <div class="tabbable">
                <div class="profile-user-info profile-user-info-striped">
                    <div class="profile-info-row">
                        <div class="profile-info-name">功能编号：</div>
                        <div class="profile-info-value">
                            <input type="text" id="id" name="id"
                                   class="col-xs-10 col-sm-8 " placeholder="字母数字和下划线组成"/>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">功能名称：</div>
                        <div class="profile-info-value">
                            <input type="text" id="name" name="name"
                                   class="col-xs-10 col-sm-8" placeholder="汉字字母数字或下划线组成"/>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">功能URL：</div>
                        <div class="profile-info-value">
                            <input type="text" id="url" name="url"
                                   class="col-xs-10 col-sm-8" placeholder="类似/sf/create*的格式"/>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">是否可配权限：</div>
                        <div class="profile-info-value">
                            <input type="checkbox" id="hasPermission" name="hasPermission"/>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">排序字段：</div>
                        <div class="profile-info-value">
                            <input type="text" id="order" name="order"
                                   class="col-xs-10 col-sm-8" placeholder="数字组成"/>
                        </div>
                    </div>
                    <div class="profile-info-row">
                        <div class="profile-info-name">所属模块：</div>
                        <div class="profile-info-value">
                            <select id="module" name="module"
                                    class="form-control col-xs-10 col-sm-8">
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>


<script type="text/javascript">

    var form = "function_form";
    var $form = $("#" + form);

    $(function () {
        //加载功能
        fillForm();
    });

    //加载模块并根据提供的模块编号设置选中状态
    function loadModule(moduleId) {
        $.ajax({
            type: 'GET',
            url: '${ctx}/${frameworkPath}/module/get',
            dataType: 'json',
            success: function (data) {
                var dataObj = eval("(" + data + ")");
                var tempAjax = "";
                $.each(dataObj, function (idx, item) {
                    if (moduleId == item.id) {
                        tempAjax += "<option selected='true' value='" + item.id + "'>" + item.name + "</option>";
                    }
                    else {
                        tempAjax += "<option value='" + item.id + "'>" + item.name + "</option>";
                    }
                })
                $("#module").empty();
                $("#module").append(tempAjax);
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert('error');
            }
        });
    }

    function fillForm() {
        $.ajax({
            type: 'GET',
            url: '${ctx}/${frameworkPath}/function/view/${param.id}',
            dataType: 'json',
            success: function (data) {
                loadData(data.rows, $form);
                //填充module下拉
                loadModule(data.rows['module']);
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                bootbox.alert('加载数据错误');
            }
        });
    }

    jQuery(function ($) {
        $form.validate({
            rules: {
                'name': {
                    required: true
                },
                'order': {
                    required: true
                },
                'id': {
                    required: true
                }
            },

            messages: {
                'name': {
                    required: "名称不能为空"
                },
                'order': {
                    required: "排序字段不能为空"
                },
                'id': {
                    required: "编号不能为空"
                }
            }
        });

    });

    function formValid() {
        var retObj = new Object();
        var valid = $("#function_form").valid();
        if (valid) {
            $form.ajaxSubmit({
                url: "${ctx}/${frameworkPath}/function/edit/${param.id}",
                type: "post",
                async: false,
                dataType: 'json',
                success: function (message) {
                    if (message.success) {
                        retObj.text = "保存成功！";
                        retObj.state = true;
                    } else {
                        retObj.text = "保存失败！";
                        retObj.state = false;
                    }
                }
            });
        } else {
            return;
        }
        return retObj;
    }
</script>
</body>
</html>